// 获取用户名
(function get_username() {
    let username_doms = document.querySelectorAll('.user-name');
    let usericon_doms = document.querySelectorAll('.user-icon img');
    if (username_doms.length == 0 || usericon_doms.length == 0) {
        setTimeout(get_username, 100);
        return ;
    }
    let username = localStorage.getItem('username');
    // 页面显示用户名
    for (let i = 0; i < username_doms.length; i++) {
        username_doms[i].innerText = username;
    }
    // 页面显示用户头像
    for (let i = 0; i < usericon_doms.length; i++) {
        usericon_doms[i].src = `/images/usericon/${username}.jpg`;
    }
})();

// 设置退出登录的按钮
(function set_quit_login_btn(params) {
    let quit_btn = document.querySelector('.user-quit');
    if (!quit_btn) {
        setTimeout(set_quit_login_btn, 100);
        return ;
    }
    quit_btn.addEventListener('click', function (e) {
        localStorage.removeItem('jwt');
        localStorage.removeItem('username');
        jmp_page('/');
    });
})();

// 加载动态
(function getAlldynamic() {
    let dynamic_box = document.querySelector('.paper-list');
    if (!dynamic_box) {
        setTimeout(getAlldynamic, 100);
        return ;
    }
    let jwt = localStorage.getItem('jwt');
    const xhr = new XMLHttpRequest();
    xhr.onload = function () {
        if (xhr.status == 200) {
            let res_data = JSON.parse(xhr.responseText);
            console.log(res_data);
            for (let i = 0; i < res_data.length; i++) {
                let dynamic_dom = document.createElement('div');
                dynamic_dom.className = 'paper-item';
                let paper_username = document.createElement('div');
                paper_username.className = 'paper-username';
                paper_username.innerHTML = res_data[i].name;
                dynamic_dom.appendChild(paper_username);
                let paper_content = document.createElement('div');
                paper_content.className = 'paper-content';
                paper_content.innerHTML = res_data[i].content;
                dynamic_dom.appendChild(paper_content);
                dynamic_dom.addEventListener('click', function (e) {
                    jmp_page(`/show_one_dynamic.html?id=${res_data[i].id}`);
                });
                dynamic_box.appendChild(dynamic_dom);
            }
        }
    };
    let data = {jwt};
    xhr.open('POST', '/dynamic/showAlldynamic', true);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(JSON.stringify(data));
})();

// 设置添加图片的按钮
// (function set_add_img_btn() {
//     let add_img_btn = document.querySelector('.inputImg-btn');

//     if (!add_img_btn) {
//         setTimeout(set_add_img_btn, 100);
//         return ;
//     }

//     add_img_btn.addEventListener('click', function (e) {
//         let input_img = document.createElement('input');
//         input_img.type = 'file';
//         input_img.accept = 'image/*';
//         input_img.addEventListener('change', function (e) {
//             let file = e.target.files[0];
//             let reader = new FileReader();
//             reader.readAsDataURL(file);
//             reader.onload = function (e) {
//                 document.querySelector('.show_input_img').style.display = 'block';
//                 document.querySelector('.show_input_img img').src = e.target.result;
//                 img_base64 = e.target.result;
//                 console.log(e.target.result);
//             }
//         });
//         input_img.click();
//     });
// })();

// 设置发布动态按钮
(function set_create_dynamic_btn() {
    let create_dynamic_btn = document.querySelector('.create-btn');

    if (!create_dynamic_btn) {
        setTimeout(set_create_dynamic_btn, 100);
        return ;
    }

    create_dynamic_btn.addEventListener('click', function (e) {
        let content = document.querySelector('.paper-content').innerText;
        let uid = localStorage.getItem('uid');
        let jwt = localStorage.getItem('jwt');
        let look = document.querySelector('.look_box input').checked ? 1 : 0;

        let data = {
            uid: uid,
            content: content,
            look: look,
            jwt: jwt
        };
        console.log(data);

        const xhr = new XMLHttpRequest();
        xhr.onload = function () {
            if (xhr.status == 200) {
                location.reload();
            }
        };
        xhr.open('POST', '/dynamic/create', true);
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.send(JSON.stringify(data));
    });
})();